package logger

import (
	"github.com/natefinch/lumberjack"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"os"
	"strings"
)

var log *zap.SugaredLogger

type Log struct{}

func (g *Log) Printf(format string, args ...interface{}) {

	log.Warn(args...)
}
func (g *Log) Println(args ...interface{}) {

	log.Debug(args...)
}

var debug = true

func init() {

	hook := lumberjack.Logger{
		Filename:   "logs/app.log", // 日志文件路径
		MaxSize:    128,            // 每个日志文件保存的大小 单位:M
		MaxAge:     7,              // 文件最多保存多少天
		MaxBackups: 30,             // 日志文件最多保存多少个备份
		Compress:   false,          // 是否压缩
	}

	encoder := zapcore.EncoderConfig{
		MessageKey:     "msg",
		LevelKey:       "level",
		TimeKey:        "time",
		NameKey:        "logger",
		CallerKey:      "file",
		StacktraceKey:  "stacktrace",
		LineEnding:     zapcore.DefaultLineEnding,
		EncodeLevel:    zapcore.LowercaseColorLevelEncoder,
		EncodeTime:     zapcore.ISO8601TimeEncoder,
		EncodeDuration: zapcore.SecondsDurationEncoder,
		EncodeCaller:   zapcore.ShortCallerEncoder, // 短路径编码器
		EncodeName:     zapcore.FullNameEncoder,
	}

	var writes = []zapcore.WriteSyncer{zapcore.AddSync(&hook)}

	if debug {
		writes = append(writes, zapcore.AddSync(os.Stdout))
	}

	core := zapcore.NewCore(zapcore.NewConsoleEncoder(encoder),
		zapcore.NewMultiWriteSyncer(writes...),
		zapcore.DebugLevel,
	)

	//var allCore []zapcore.Core
	//conis := zapcore.NewCore(encoder, zapcore.AddSync(os.Stdout), zapcore.DebugLevel)
	//coref := zapcore.NewCore(encoder, writes, zapcore.DebugLevel)
	//allCore = append(allCore, conis)
	//allCore = append(allCore, coref)
	//core := zapcore.NewTee(allCore...)

	logger := zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1))

	log = logger.Sugar()

}

func formatLog(v ...interface{}) string {
	var msg string = ""

	if len(v) == 0 {
		return msg
	}

	msg += strings.Repeat(" %v", len(v))

	return msg
}

func Info(args ...interface{}) {

	log.Infof(formatLog(args...), args...)
	//go PushMsg(args...)
}

func Warn(args ...interface{}) {

	log.Warnf(formatLog(args...), args...)
	//go PushMsg(args...)
}

func Error(args ...interface{}) {

	log.Errorf(formatLog(args...), args...)
	//go PushMsg(args...)
}

func Fatal(args ...interface{}) {

	log.Fatalf(formatLog(args...), args...)
	//go PushMsg(args...)
}

func Debug(args ...interface{}) {

	log.Debugf(formatLog(args...), args...)
	//go PushMsg(args...)
}

func Print(args ...interface{}) {

	log.Infof(formatLog(args...), args...)
	//go PushMsg(args...)
}

func Println(args ...interface{}) {

	log.Infof(formatLog(args...), args...)
}
